import printer.BinaryTrees;

import java.util.Comparator;
import java.util.Objects;
import java.util.Random;

public class Main {

    public static void main(String[] args) {

        Integer[] ageArray = Integers.random(10, 10, 99);

        BinarySearchTree<Person> bst = new BinarySearchTree<>(new Comparator<>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getAge() - o2.getAge();
            }
        });

        for (Integer age : Objects.requireNonNull(ageArray)) {
            bst.add(new Person(age));
        }

        System.out.println("中序遍历:");
        bst.inorderTraversal(new Visitor<>() {
            @Override
            public void visit(Person element) {
                System.out.printf("%3d", element.getAge());
            }
        });

        System.out.println();
        System.out.println("前序遍历:");
        bst.preorderTraversal(new Visitor<>() {
            @Override
            public void visit(Person element) {
                System.out.printf("%3d", element.getAge());
            }
        });
        System.out.println();

        System.out.println("后序遍历:");
        bst.postorderTraversal(new Visitor<>() {
            @Override
            public void visit(Person element) {
                System.out.printf("%3d", element.getAge());
            }
        });
        System.out.println();

        System.out.println("层序遍历:");
        bst.levelOrderTraversal(new Visitor<>() {
            @Override
            public void visit(Person element) {
                System.out.printf("%3d", element.getAge());
            }
        });

        System.out.println();
        System.out.println("高度_递归实现:" + bst.height_recu());
        System.out.println("高度_非递归实现:" + bst.height_iter());

        {
            int age = ageArray[ageArray.length >> 1];
            Person predecessor = bst.predecessor(new Person(age));
            Person successor = bst.successor(new Person(age));
            System.out.println("值为" + age + "的节点的前驱节点 = " + (predecessor == null ? null : predecessor.getAge()));
            System.out.println("值为" + age + "的节点的后继节点 = " + (successor == null ? null : successor.getAge()));
        }

        BinaryTrees.print(bst);
        System.out.println();

        bst.remove(new Person(38));
        BinaryTrees.print(bst);
    }
}
